﻿#include "mainwindow.h"
#include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    demo1();
    demo2();
    demo3();
    demo4();
    demo5();
    demo6();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::demo1()
{
    //自定义标题
    QStringList head;
    head << tr("参数") << tr("数值");
    ui->widget->setHeaderLabels(head);

    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(235);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setAttribute(QLatin1String("decimals"), 5);
    item->setValue(3.1415926);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::String, tr("字符串数据"));
    item->setValue(tr("你好!世界!"));
    QtBrowserItem *p = ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Color, tr("颜色数据"));
    item->setValue(QColor(qRgb(255, 0, 0)));
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Size, tr("宽高数据"));
    item->setValue(QSize(10, 10));
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::PointF, tr("坐标数据"));
    item->setValue(QPointF(10, 10));
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), tr("列表数据"));
    QStringList enumNames;
    enumNames << "Enum0"
              << "Enum1"
              << "Enum2"
              << "Enum3";
    item->setAttribute(QLatin1String("enumNames"), enumNames);

    QMap<int, QIcon> enumIcons;
    enumIcons[0] = QIcon(":/images/up.png");
    enumIcons[1] = QIcon(":/images/right.png");
    enumIcons[2] = QIcon(":/images/down.png");
    enumIcons[3] = QIcon(":/images/left.png");
    QVariant v;
    v.setValue(enumIcons);
    item->setAttribute(QLatin1String("enumIcons"), v);
    item->setValue(1);
    ui->widget->addProperty(item);

    //背景色
    ui->widget->setBackgroundColor(p, QColor(qRgb(255, 255, 0)));
}

void MainWindow::demo2()
{
    //自定义标题
    QStringList head;
    head << tr("属性") << tr("数值");
    ui->widget_2->setHeaderLabels(head);

    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget_2);

    //组1
    QtProperty *groupItem = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), tr("组1"));
    ui->widget_2->addProperty(groupItem);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(567);
    groupItem->addSubProperty(item);

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    groupItem->addSubProperty(item);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(3.1415926);
    groupItem->addSubProperty(item);

    item = pVarManager->addProperty(QVariant::String, tr("字符串数据"));
    item->setValue(tr("你好!世界!"));
    groupItem->addSubProperty(item);

    //组2
    QtProperty *groupItem2 = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), tr("组2"));
    ui->widget_2->addProperty(groupItem2);
    pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(2514.495);
    groupItem2->addSubProperty(item);

    //折叠
    QSet<QtProperty *> p = pVarManager->properties();
    QList<QtBrowserItem *> list = ui->widget_2->items(groupItem);
    ui->widget_2->setExpanded(list.at(0), false);

    //背景色
    ui->widget_2->setBackgroundColor(list.at(0), QColor(qRgb(200, 200, 200)));
}

void MainWindow::demo3()
{
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget_3);
    QtVariantEditorFactory *pVarFactory = new QtVariantEditorFactory(ui->widget_3);

    connect(pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged);

    QtProperty *groupItem = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), tr("组1"));

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(123);
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("整形数据");
    m_listProperty << item;

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("布尔型数据");
    m_listProperty << item;

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(3.1415926);
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("浮点数据");
    m_listProperty << item;

    item = pVarManager->addProperty(QVariant::String, tr("字符串数据"));
    item->setValue(tr("你好!世界!"));
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("字符串数据");
    m_listProperty << item;

    item = pVarManager->addProperty(QVariant::Color, tr("颜色数据"));
    item->setValue(QColor(qRgb(255, 0, 0)));
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("颜色数据");
    m_listProperty << item;

    item = pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), tr("列表数据"));
    QStringList enumNames;
    enumNames << "Enum0"
              << "Enum1"
              << "Enum2"
              << "Enum3";
    item->setAttribute(QLatin1String("enumNames"), enumNames);

    QMap<int, QIcon> enumIcons;
    enumIcons[0] = QIcon(":/images/up.png");
    enumIcons[1] = QIcon(":/images/right.png");
    enumIcons[2] = QIcon(":/images/down.png");
    enumIcons[3] = QIcon(":/images/left.png");
    QVariant v;
    v.setValue(enumIcons);
    item->setAttribute(QLatin1String("enumIcons"), v);
    item->setValue(1);
    groupItem->addSubProperty(item);
    m_property_dic[item] = tr("列表数据");
    m_listProperty << item;

    //Add
    ui->widget_3->addProperty(groupItem);
    ui->widget_3->setFactoryForManager(pVarManager, pVarFactory);

    //遍历字段和修改
    QSet<QtProperty *> s = pVarManager->properties();
    QSet<QtProperty *>::iterator iter = s.begin();
    QtVariantProperty *varPro = nullptr;
    int type1 = 0;
    int type2 = 0;
    for (; iter != s.end(); iter++)
    {
        varPro = static_cast<QtVariantProperty *>(*iter);
        type1 = varPro->valueType();
        type2 = varPro->propertyType(); //QVariant的索引
        qDebug() << varPro->value() << type1 << type2;
        varPro->setValue(5);
    }

    //指定某个字段修改
    m_listProperty.at(2)->setValue(1111);
}

void MainWindow::demo4()
{
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget_4);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(235);
    ui->widget_4->addProperty(item);

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    ui->widget_4->addProperty(item);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(3.1415926);
    ui->widget_4->addProperty(item);

    item = pVarManager->addProperty(QVariant::Color, tr("颜色数据"));
    item->setValue(QColor(qRgb(255, 0, 0)));
    ui->widget_4->addProperty(item);
}

void MainWindow::demo5()
{
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget_5);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(235);
    ui->widget_5->addProperty(item);

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    ui->widget_5->addProperty(item);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(3.1415926);
    ui->widget_5->addProperty(item);

    item = pVarManager->addProperty(QVariant::Color, tr("颜色数据"));
    item->setValue(QColor(qRgb(255, 0, 0)));
    ui->widget_5->addProperty(item);
}

void MainWindow::demo6()
{
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget_6);
    QtVariantEditorFactory *pVarFactory = new QtVariantEditorFactory(ui->widget_6);
    ui->widget_6->setFactoryForManager(pVarManager, pVarFactory);
    connect(pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged2);

    //组1
    QtProperty *groupItem = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), tr("组1"));
    ui->widget_6->addProperty(groupItem);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(123);
    groupItem->addSubProperty(item);
    m_property_dic2[item] = tr("整形数据");

    item = pVarManager->addProperty(QVariant::Color, tr("颜色数据"));
    item->setValue(QColor(qRgb(255, 0, 0)));
    groupItem->addSubProperty(item);
    m_property_dic2[item] = tr("颜色数据");

    //组2
    QtProperty *groupItem2 = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), tr("组2"));
    ui->widget_6->addProperty(groupItem2);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setValue(2514.495);
    groupItem2->addSubProperty(item);
    m_property_dic2[item] = tr("浮点数据");
}

void MainWindow::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
{
    QString s = m_property_dic[property];
    qDebug() << s << ":" << value;
}

void MainWindow::variantPropertyValueChanged2(QtProperty *property, const QVariant &value)
{
    QString s = m_property_dic2[property];
    qDebug() << s << ":" << value;
}
